function isEmail(value) {
    return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value);
}
function isNumeric(num) {
    return !isNaN(num);
}

function clean() {
    $('#newPasswordField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#nameField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#identityNumberField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#emailField').parent().removeClass('has-error has-feedback').find('span').remove();
}

function validate() {
    clean();
    var validator = {valid: false};
    var newPasswordField = $('#newPasswordField').val();
    if (newPasswordField.length > 70) {
        $('#newPasswordField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Password must have 1-70 characters!</span>').insertAfter('#newPasswordField');
        return validator;
    }


    var nameField = $('#nameField').val();
    if (!nameField) {
        $('#nameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Name must have 1-100 characters!</span>').insertAfter('#nameField');
        return validator;
    }
    if (nameField.length > 70) {
        $('#nameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Name must have 1-100 characters!</span>').insertAfter('#nameField');
        return validator;
    }


    var identityNumberField = $('#identityNumberField').val();
    if (!identityNumberField) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }
    if (identityNumberField.length > 11 || identityNumberField.length < 8) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }
    if (!isNumeric(identityNumberField)) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }

    var emailField = $('#emailField').val();
    if (!emailField) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Email must have 1-60 characters!</span>').insertAfter('#emailField');
        return validator;
    }
    if (emailField.length > 70) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Email must have 1-60 characters!</span>').insertAfter('#emailField');
        return validator;
    }
    if (!isEmail(emailField)) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid Email adress!</span>').insertAfter('#emailField');
        return validator;
    }
    
    validator["passwordField"] = newPasswordField;
    validator["nameField"] = nameField;
    validator["identityNumberField"] = identityNumberField;
    validator["emailField"] = emailField;

    validator["valid"] = true;
    return validator;
}

function onClickEditAdminAccount(eventObject) {
    var validator = validate();
    if (validator["valid"]) {
        sendEditAdmin(validator, function (data) {
            if (data["success"] === true) {
                window.location = '/Trabalho_PD/homepage.html';
            } else {
                $("#registerUserButtonArea").append('<span class="glyphicon glyphicon-remove form-control-feedback"></span><span class="help-block">Error! Retry please!</span>');
            }
        });
    }
}


